package com.itestor.iptv.modules.ne.actions;


import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import com.itestor.iptv.global.actions.AbstractPagedQueryAction;
import com.itestor.iptv.global.bean.AbstractPagedQuery;
import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.vo.RegionManageBean;
import com.itestor.iptv.modules.ne.bean.RobotChooseQuery;
import com.itestor.iptv.modules.ne.service.RobotManageService;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotChooseQueryAction extends AbstractPagedQueryAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RobotChooseQueryAction.class);

    private RobotManageService robotManageService;
    
    private RegionManageService regionManageService;
    
    /**
     * 获取选择设备列表.
     * @param form 
     * @param request 
     * @param pagerCount 
     * @param pagerOffset 
     * @return AbstractPagedQuery
     * @throws Exception  
     */   
    @SuppressWarnings("unchecked")
    public AbstractPagedQuery doPagedQuery(ActionForm form,
            HttpServletRequest request, int pagerCount, int pagerOffset)
            throws Exception {
        
        String queryIdRegion = request.getParameter("queryIdRegion");
        String queryIdSubRegion = request.getParameter("queryIdSubRegion");        
        String queryRobotName=request.getParameter("queryRobotName");
        
        String queryIdGroup=request.getParameter("queryIdGroup");
        
        
        RobotChooseQuery robotChooseQuery = new RobotChooseQuery();        
        robotChooseQuery.setPagerCount(pagerCount);
        robotChooseQuery.setPagerOffset(pagerOffset);
        
        List<Integer> regionIdList=new ArrayList<Integer>();
        
        // 查找区局列表
        if (!StringUtils.isEmpty(queryIdSubRegion)) {
            Integer regionId =Integer.parseInt(queryIdRegion);   
            List<RegionManageBean> regionList=regionManageService.getRegionListByParentid(regionId);            
            regionIdList=convertRegionListToIds(regionList);
            
            //add owner
            regionIdList.add(regionId);
        }else if (!StringUtils.isEmpty(queryIdRegion)) {
            Integer regionId =Integer.parseInt(queryIdRegion);            
            List<RegionManageBean> regionList =regionManageService.getRegionListByParentid(regionId);
            regionIdList=convertRegionListToIds(regionList);
            
            //add owner
            regionIdList.add(regionId);
        }
        
        if (!StringUtils.isEmpty(queryRobotName)) {
            robotChooseQuery.setRobotName(queryRobotName);                
        }     
        
        if (!regionIdList.isEmpty()){        
            robotChooseQuery.setIdRegionlist(regionIdList);
        }
        
        
        robotManageService.queryRobotChooseList(robotChooseQuery); 
        
        List<RegionManage> regionList= regionManageService.getRootRegionList();
        request.setAttribute("RA_ROOT_REGIONS", regionList);
        
        
        request.setAttribute("queryIdRegion", queryIdRegion);
        request.setAttribute("queryIdSubRegion", queryIdSubRegion);
        request.setAttribute("queryRobotName", queryRobotName);
        request.setAttribute("queryIdGroup", queryIdGroup);
        
        
        return robotChooseQuery;
    }
    
    /**
     * 生成region.id列表
     * @param regionList
     * @return List
     */
    private List<Integer> convertRegionListToIds(List<RegionManageBean> regionList){
        List<Integer> list=new ArrayList<Integer>();        
        for (RegionManageBean bean: regionList){
            list.add(bean.getIdRegion());
        }
        return list;
    }
    
    
    /**
     * 　
     * @param pRobotManageService
     */
    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }
    /**
     * 
     * @param pRegionManageService
     */
    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }
    
    

}